package com.zmc.lostfound.common.feign.fallback;

import com.zmc.lostfound.common.feign.AuthFeign;
import com.zmc.lostfound.common.utils.Result;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ZMC
 * @description Auth用户认证服务中的api远程调用失败后的降级逻辑
 */
@Slf4j
@Component
public class AuthFallbackFactory implements FallbackFactory<AuthFeign> {

    /**
     * @param throwable 熔断的异常信息
     * @return AuthFeign
     * @description 当Auth用户认证服务中的api产生异常不可用时，发生熔断，上游服务就会执行降级逻辑
     */
    @Override
    public AuthFeign create(Throwable throwable) {
        return new AuthFeign() {

            @Override
            public Result queryBaseInfoByIds(List<Long> userIds) {
                log.error("远程调用auth服务的【/auth/user/query/baseInfoByIds】API时发生熔断 异常信息: {}", throwable.getMessage(), throwable);
                return Result.error("远程调用auth服务的【/auth/user/query/baseInfoByIds】API时发生熔断").put("errorInfo", throwable.getMessage());
            }

            @Override
            public Result queryPartInfoByIds(List<Long> userIds) {
                log.error("远程调用auth服务的【/auth/user/query/partInfoByIds】API时发生熔断 异常信息: {}", throwable.getMessage(), throwable);
                return Result.error("远程调用auth服务的【/auth/user/query/partInfoByIds】API时发生熔断").put("errorInfo", throwable.getMessage());
            }

            @Override
            public Result queryDetailInfoById(Long id) {
                log.error("远程调用auth服务的【/auth/user/query/detailInfoById/{id}】API时发生熔断 异常信息: {}", throwable.getMessage(), throwable);
                return Result.error("远程调用auth服务的【/auth/user/query/detailInfoById/{id}】API时发生熔断").put("errorInfo", throwable.getMessage());
            }

            @Override
            public Result queryBaseInfoById(Long id) {
                log.error("远程调用auth服务的【/auth/user/query/baseInfoById/{id}】API时发生熔断 异常信息: {}", throwable.getMessage(), throwable);
                return Result.error("远程调用auth服务的【/auth/user/query/baseInfoById/{id}】API时发生熔断").put("errorInfo", throwable.getMessage());
            }

            @Override
            public Result queryRoleAndPermissionById(Long id) {
                log.error("远程调用auth服务的【/auth/user/query/roleAndPermission/{id}】API时发生熔断 异常信息: {}", throwable.getMessage(), throwable);
                return Result.error("远程调用auth服务的【/auth/user/query/roleAndPermission/{id}】API时发生熔断").put("errorInfo", throwable.getMessage());
            }

        };
    }

}
